package com.rbac.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rbac.modules.system.entity.SysRolePermission;
import com.rbac.modules.system.mapper.SysRolePermissionMapper;
import com.rbac.modules.system.service.SysRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements SysRolePermissionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRolePermission(Long roleId, Long permissionId) {
        SysRolePermission rolePermission = new SysRolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionId);
        save(rolePermission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRolePermission(Long roleId, Long permissionId) {
        lambdaUpdate()
            .eq(SysRolePermission::getRoleId, roleId)
            .eq(SysRolePermission::getPermissionId, permissionId)
            .remove();
    }

    @Override
    public List<Long> getPermissionIdsByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRolePermission::getRoleId, roleId);
        
        return list(wrapper).stream()
                .map(SysRolePermission::getPermissionId)
                .collect(Collectors.toList());
    }
} 